package com.example.day4retrofit;

import java.util.ResourceBundle;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class RetrofitManager {
    private static RetrofitManager retrofitManager;
    public static RetrofitManager getInstance(){
        if(retrofitManager==null){
            retrofitManager=new RetrofitManager();
        }
        return retrofitManager;
    }


    private String baseUrl="https://news.163.com/special/new2016_rbeijing_api/";

    public void setBaseUrl(String baseUrl) {
        this.baseUrl = baseUrl;
    }

    private Retrofit retrofit;
    public Retrofit getRetrofit() {
        if(retrofit==null){
            create();
        }
        return retrofit;
    }
    public void create(){
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient client = new OkHttpClient.Builder()
                .addInterceptor(loggingInterceptor).build();
         retrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .client(client)
                //请求执行线程的方式
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                //最数据解析
                .addConverterFactory(GsonConverterFactory.create())
                .build();


    }
}
